package edu.kit.csl.cratyli.datatypes;

public class Word implements Comparable<Word> {

	// Written representation of the word
	private String ortho;
	// Pronunciation (each element is one phoneme)
	private String[] pronun;
	// String representation of the pronunciation
	private String strPronun;
	// Weight (approximately frequency)
	private double weight = 0.0;
	
	/**
	 * Creates a new word instance.
	 * 
	 * @param written the orthographic representation of the word
	 * @param pronun the pronunciation as blank separated string
	 */
	public Word(String ortho, String pronun) {
		this.ortho = ortho;
		this.strPronun = pronun.trim();
		this.pronun = strPronun.split(" ");
	}

	/**
	 * Get the orthographic representation of the word.
	 * 
	 * @return the written representation
	 */
	public String getOrthographic() {
		return ortho;
	}

	/**
	 * Get the pronunciation as string array. Each element contains a single
	 * written phoneme.
	 * 
	 * @return the pronunciation of the word
	 */
	public String[] getPronunciation() {
		return pronun;
	}

	/**
	 * Set a new pronunciation.
	 * 
	 * @param pronun the new pronunciation
	 */
	public void setPronunciation(String[] pronun) {
		this.pronun = pronun;
		StringBuilder b = new StringBuilder();
		for (String p : pronun) {
			b.append(" " + p);
		}
		strPronun = b.toString().trim();
	}
	
	/**
	 * Get the pronunciation as blank separated string.
	 * 
	 * @return a string describing the pronunciation
	 */
	public String getStringPronunciation() {
		return strPronun;
	}

	/**
	 * Get the weight of this word.
	 * 
	 * @return the current weight
	 */
	public double getWeight() {
		return weight;
	}
	
	/**
	 * Increase the weight by one.
	 */
	public void increaseWeight() {
		weight++;
	}
	
	/**
	 * Increase the weight.
	 * 
	 * @param inc factor to add
	 */
	public void increaseWeight(double inc) {
		weight += inc;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return strPronun.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		return strPronun.equals(((Word) obj).strPronun);
	}

	/**
	 * Set a new orthographic representation of this word.
	 * 
	 * @param ortho the new written representation
	 */
	public void setOrthographic(String ortho) {
		this.ortho = ortho;
	}

	/**
	 * @param weight the weight to set
	 */
	public void setWeight(double weight) {
		this.weight = weight;
	}

	@Override
	public int compareTo(Word arg0) {
		return arg0.weight > weight ? 1 : 
			(arg0.weight < weight ? -1 : 0);
	}
}
